package com.opticon.vaadinR.factory;

import com.opticon.vaadinR.core.conf.CoreConfiguration;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.IOException;

import static java.lang.String.format;

/**
 * This class is a
 */
public class TmpFileFactory {


    private static Logger logger = Logger.getLogger(TmpFileFactory.class);

    public static File makePNG() throws IOException {

        File lTmpDir = CoreConfiguration.getTmpDir();
        long lTime = System.currentTimeMillis();

        String lFilename = format("%d_.png", lTime);

        File lResult = new File(lTmpDir, lFilename);
        lResult.createNewFile();
        return (lResult);

    }

    public static File getSessionTaskFolder(String aSessionID) {
        File lTmpDir = CoreConfiguration.getTmpDir();

        File lSessionDir = new File(lTmpDir, aSessionID);
        if (!lSessionDir.exists()) {
            boolean lMkdir = lSessionDir.mkdir();
            if (!lMkdir) {
                logger.debug("could not create session directory");
            }
        }

        File lTaskDir = new File(lSessionDir, String.valueOf(System.currentTimeMillis()));
        if (lTaskDir.exists() == false) {
            boolean lMkdir = lTaskDir.mkdir();
            if (!lMkdir) {
                logger.debug("could not create task directory");
            }

        }
        return lTaskDir;
    }

}
